package Gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import auth.User;
import auth.UserFuncs;

public class Login {
	public Display loginDisplay;
	private Shell loginShell;
	public Login(Shell shell, final Main main){
		this.loginDisplay = shell.getDisplay();
		this.loginShell = new Shell (loginDisplay);
		main.addShell(loginShell);
		invokeLogin(shell, loginShell, loginDisplay, main);
	}
	public static void invokeLogin(Shell shell, final Shell loginShell, Display loginDisplay, final Main main){
		final Shell passShellForward = shell;
		GridLayout gl = new GridLayout(1,true);
		loginShell.setLayout(gl);
		loginShell.setText("Login");
		loginShell.setLocation(shell.getLocation().x + 50, shell.getLocation().y + 50);
	
		Label Explain = new Label(loginShell, SWT.NONE);
		Explain.setLayoutData(new GridData(GridData.BEGINNING,GridData.CENTER,false,true));
		Explain.setText("Please enter your username and password:");
		
		final Label Username = new Label (loginShell, SWT.NONE);
		Username.setLayoutData(new GridData(GridData.BEGINNING,GridData.CENTER,false,false));
		Username.setText ("Username:\t");
		final Text UsernameInput = new Text(loginShell,SWT.BORDER);
		UsernameInput.setLayoutData(new GridData(GridData.FILL,GridData.CENTER,true,false));
	
		final Label Password = new Label (loginShell, SWT.NONE);
		Password.setLayoutData(new GridData(GridData.BEGINNING,GridData.CENTER,false,false));
		Password.setText ("Password:\t");
		final Text PasswordInput = new Text(loginShell,SWT.BORDER);
		PasswordInput.setLayoutData(new GridData(GridData.FILL,GridData.CENTER,true,false));
		PasswordInput.setEchoChar('*');
	
		final Button loginBt = new Button(loginShell,0);
		loginBt.setText("Login");
		loginBt.setLayoutData(new GridData(GridData.FILL,GridData.CENTER,true,true));
		
		final Button cancelBt = new Button(loginShell,0);
		cancelBt.setText("Cancel");
		cancelBt.setLayoutData(new GridData(GridData.FILL,GridData.CENTER,true,true));
		
		Listener listener = new Listener () {
			public void handleEvent (Event e) {
				switch (e.type) {
				case SWT.Close:
					main.removeShell(loginShell);
					main.enableLogin();
					main.disableLogout();
					main.enableReg();
					loginShell.dispose();
					break;
				default:
		        }	
		   }
		};
		loginShell.addListener (SWT.Close, listener);
		
		cancelBt.addSelectionListener(
				new SelectionAdapter() {
					public void widgetSelected(SelectionEvent e) {
						main.removeShell(loginShell);
						main.enableLogin();
						main.disableLogout();
						main.enableReg();
						loginShell.dispose();
					}
				}
		);
		
		loginBt.addSelectionListener(
				new SelectionAdapter() {
					public void widgetSelected(SelectionEvent e) {
						String tempUN = UsernameInput.getText();
						String tempPass = PasswordInput.getText();
						int iErr = 0;
						if(tempUN == ""){
							iErr = 1;
							if(tempPass == ""){
								MessageDialog.Error(loginShell, "Error", "You forgot to enter your username and password!");
							}else{
								MessageDialog.Error(loginShell, "Error", "You forgot to enter your username!");
							}
						}else if(tempPass == ""){
							iErr = 1;
							MessageDialog.Error(loginShell, "Error", "You forgot to enter your password!");
						}
						User loggedInUser = UserFuncs.getUser(tempUN, tempPass);
						auth.Identity.setIdentity(loggedInUser);
						if(loggedInUser == null){
							iErr = 1;
							MessageDialog.Error(loginShell, "Error", "User does not exist, check you username and password");
						}
						if(iErr != 1){
							loggedInUser.loadList();
							main.removeShell(loginShell);
							main.setLogged();
							main.setUser(loggedInUser);
							main.disableReg();
							main.enableLogout();
							loginShell.dispose();
						}
					}
				}
		);
		
		loginShell.pack();
		loginShell.open();

		while (!loginShell.isDisposed()) {
			if (!loginDisplay.readAndDispatch())
				loginDisplay.sleep();
		}
	}
}
